﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;
using PH.Model.Results;
using PH.Model.Achievements;
using PH.Logic.Achievements;
using PH.Logic.Systems;
using PH.Model.Systems;
namespace PH.Web.Management.Areas.Achievements.Controllers
{
    [RouteArea("Achievements")]
    [RoutePrefix("Archive")]
    public class ArchiveApiController : ApiController
    {
        [Route("Gets")]
        [HttpPost]
        public JsonEntitySet<ArchiveModel> Gets(JObject request)
        {
            string errinfo = "no data";
            try
            {
                ArchiveQuery sea = new ArchiveQuery();
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.PageSize = request.Value<int>("pageSize");
                sea.AType = request.Value<byte>("aType");
                sea.GetMarkCount = request.Value<bool>("getMarkCount");
                sea.Expired = request.Value<bool?>("expired");
               
                if (Global.WebContext.CurrentUser.Roles.Any(p => p.RoleName == "Bureau"))
                    sea.BureauID = Global.WebContext.CurrentUser.AssociatedBureauID.Value;
                else
                    sea.SchoolID = Global.WebContext.CurrentUser.AssociatedSchoolID.Value;
                sea.Key = request.Value<string>("key");
                var result = ArchiveLogic.Gets(sea);
                return new JsonEntitySet<ArchiveModel>(result);
            }
            catch (Utils.PhException pex) { errinfo = pex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<ArchiveModel>(null, errinfo, false);
        }

        [Route("GetMyArchives")]
        [HttpPost]
        public JsonEntitySet<ArchiveModel> GetMyArchives(JObject request)
        {
            string errinfo = "no data";
            try
            {
                ArchiveQuery sea = new ArchiveQuery();
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.PageSize = request.Value<int>("pageSize");
                sea.AType = request.Value<byte>("aType");
                sea.GetMarkCount = request.Value<bool>("getMarkCount");
                if (Global.WebContext.CurrentUser.Roles.Any(p => p.RoleName == "Bureau"))
                    sea.BureauID = Global.WebContext.CurrentUser.AssociatedBureauID.Value;
                else
                    sea.SchoolID = Global.WebContext.CurrentUser.AssociatedSchoolID.Value;
                sea.Key = request.Value<string>("key");
                var result = ArchiveLogic.GetMyArchives(sea);
                return new JsonEntitySet<ArchiveModel>(result);
            }
            catch (Utils.PhException pex) { errinfo = pex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<ArchiveModel>(null, errinfo, false);
        }

        [Route("SaveArchive")]
        [HttpPost]
        public JsonEntity<ArchiveModel> SaveArchive(JObject request)
        {
            string errinfo = "对不起！保存失败，出错原因：";
            try
            {
                #region 收集参数
                ArchiveModel arc = new ArchiveModel();
                var guid = request.Value<string>("guid");
                if (!string.IsNullOrEmpty(guid))
                    arc.Guid = new Guid(guid);
                arc.Title = request.Value<string>("title");
                arc.Description = request.Value<string>("description");
                arc.BeginTime = DateTime.Parse(request.Value<string>("sDate"));
                arc.EndTime = DateTime.Parse(request.Value<string>("eDate"));
                
                if (Web.Global.WebContext.CurrentUser.Roles.Any(p => p.RoleName == "Bureau")){
                    arc.BureauID = Web.Global.WebContext.CurrentUser.AssociatedBureauID;
                    arc.AType = 2;
                }
                else
                {
                    arc.AType = 1;
                    arc.SchoolID = Web.Global.WebContext.CurrentSchool.ID;
                    //arc.JoinSchools = new SchoolInArchiveCollection();
                    //arc.JoinSchools.Add(new SchoolInArchiveModel() { SchoolGuid = Web.Global.WebContext.CurrentSchool.Guid });
                }
                #endregion

                if (arc.Guid == Guid.Empty)
                    arc = ArchiveLogic.CreateNew(arc);
                else
                    ArchiveLogic.Update(arc);
                return new JsonEntity<ArchiveModel>(arc);
            }
            catch (Utils.PhException pex) { errinfo = pex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntity<ArchiveModel>(null, errinfo, false);
        }

        [Route("DeleteArchive")]
        [HttpPost]
        public JsonResult<bool> DeleteArchive(JObject request)
        {
            string errinfo = "对不起！删除失败，出错原因：";
            try
            {
                #region 收集参数
                var guid=Guid.Empty;
                if (!string.IsNullOrEmpty(request.Value<string>("guid")))
                    guid = new Guid(request.Value<string>("guid"));
                #endregion
                ArchiveLogic.Delete(guid);
                return new JsonResult<bool>(true);
            }
            catch (Utils.PhException pex) { errinfo = pex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }
    }
}
